package com.ucap.weking.guangan.Activitys;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.boredream.bdvideoplayer.BDVideoView;
import com.boredream.bdvideoplayer.bean.IVideoInfo;
import com.boredream.bdvideoplayer.listener.OnVideoControlListener;
import com.boredream.bdvideoplayer.utils.DisplayUtils;
import com.google.gson.Gson;
import com.ucap.weking.guangan.R;
import com.ucap.weking.guangan.adapter.ViewPagerAdapter;
import com.ucap.weking.guangan.bean.LiveOnlineContentBean;
import com.ucap.weking.guangan.dataUri.DataUri;
import com.ucap.weking.guangan.fragments.liveOnlineFrgmentContent.FragmentContent2;
import com.ucap.weking.guangan.fragments.liveOnlineFrgmentContent.FragmentContent4;
import com.ucap.weking.guangan.httpTools.HttpCallRequest;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class LiveOnlineListContentActivity extends AppCompatActivity {
    @InjectView(R.id.bd_vv)
    BDVideoView videoView;
    @InjectView(R.id.table)
    TabLayout table;
    @InjectView(R.id.vp_content)
    ViewPager vpContent;
    @InjectView(R.id.pb_is_loading)
    ProgressBar pbLoading;
    @InjectView(R.id.ll_is_err)
    LinearLayout llErr;
    private String id;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.black));
        }
        setContentView(R.layout.activity_live_online);
        ButterKnife.inject(this);
        Intent intent = getIntent();

        title = intent.getStringExtra("title");
        id = intent.getStringExtra("id");
        getDataForService(id);
        intiClick();

    }

    public void intiClick() {
        llErr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDataForService(id);
            }
        });
    }

    private void setContentData(LiveOnlineContentBean.RowsBean rowsBean) {
        ArrayList<Fragment> data = new ArrayList<>();
        Bundle bundle = new Bundle();
        bundle.putString("title", rowsBean.getTITLE());
        bundle.putString("timer", rowsBean.getTIME());
        bundle.putString("guest", rowsBean.getGUEST());
        bundle.putString("explain", rowsBean.getDESCRIBE());
        data.add(new FragmentContent4().setDataText(bundle));
        data.add(new FragmentContent2().setData(rowsBean.getSHILU()));
        data.add(new FragmentContent2().setData(rowsBean.getJIJIN()));
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), data, new String[]{"往期简介", "文字实录", "问题解答"});
        vpContent.setAdapter(adapter);
        table.setupWithViewPager(vpContent);
    }

    public void getDataForService(String itemId) {
        pbLoading.setVisibility(View.VISIBLE);
        vpContent.setVisibility(View.GONE);
        llErr.setVisibility(View.GONE);
        Log.i("在线访谈往期详细 ", "getDataForService: " + DataUri.getLIVE_ONLINE_CONTENT(itemId));
        HttpCallRequest.callRequest(DataUri.getLIVE_ONLINE_CONTENT(itemId), new HttpCallRequest.OnCallListener() {
            @Override
            public void onRequest(final String body) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Gson gson = new Gson();
                            LiveOnlineContentBean liveOnlineContentBean = gson.fromJson(body, LiveOnlineContentBean.class);
                            final LiveOnlineContentBean.RowsBean rowsBean = liveOnlineContentBean.getRows().get(0);
                            if (rowsBean != null) {
                                final IVideoInfo info = new IVideoInfo() {
                                    @Override
                                    public String getVideoTitle() {
                                        return title;
                                    }

                                    @Override
                                    public String getVideoPath() {
                                        return rowsBean.getVIDEO();
                                    }
                                };
                                videoView.startPlayVideo(info);
                                videoView.setOnVideoControlListener(new OnVideoControlListener() {
                                    @Override
                                    public void onBack() {
                                        onBackPressed();
                                    }

                                    @Override
                                    public void onFullScreen() {
                                        DisplayUtils.toggleScreenOrientation(LiveOnlineListContentActivity.this);
                                    }

                                    @Override
                                    public void onRetry(int errorStatus) {
                                        videoView.startPlayVideo(info);
                                    }
                                });
                                setContentData(rowsBean);
                                pbLoading.setVisibility(View.GONE);
                                vpContent.setVisibility(View.VISIBLE);

                            }
                        } catch (Exception e) {
                        }
                    }
                });

            }

            @Override
            public void onErr(Exception e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        llErr.setVisibility(View.VISIBLE);
                        pbLoading.setVisibility(View.GONE);
                        vpContent.setVisibility(View.GONE);
                    }
                });
            }
        });
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();

        videoView.onStop();
    }

    @Override
    protected void onStart() {
        super.onStart();

        videoView.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();

        videoView.onDestroy();
    }

    @Override
    public void onBackPressed() {
        if (!DisplayUtils.isPortrait(this)) {
            if (!videoView.isLock()) {
                DisplayUtils.toggleScreenOrientation(this);
            }
        } else {
            super.onBackPressed();
        }
    }

}
